﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
using GSM;

namespace GsmConcole
{
    public partial class FormMain : Form
    {
        private GsmModule gsmModule;

        public FormMain()
        {
            InitializeComponent();
        }

        private void FormMain_Load(object sender, EventArgs e)
        {
            this.comboBox_串口.DataSource = SerialPort.GetPortNames();
        }

        private void button_打开_Click(object sender, EventArgs e)
        {
            try
            {
                this.gsmModule = new GsmModule(this.comboBox_串口.Text, 9600);
                this.gsmModule.NewSMSEvent += new EventHandler<GSM.Common.ShortMessage>(gsmModule_NewSMSEvent);
                this.gsmModule.SerialPortIO += new Action<string>(gsmModule_SerialPortIO);
                if (this.gsmModule.Open())
                {
                    MessageBox.Show("打开串口成功，点击确定进行初始化!");
                    this.gsmModule.ResetGsmModel();
                }

                else
                    MessageBox.Show("打开串口失败");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void gsmModule_NewSMSEvent(object sender, GSM.Common.ShortMessage e)
        {
            string msg = string.Format("==============接收到新短信====================\r\n{0}   {1}     \r\n{2}", e.PhoneNo, e.Time.ToString(), e.Content);
            this.gsmModule_SerialPortIO(msg);
        }

        private void gsmModule_SerialPortIO(string obj)
        {
            if (this.InvokeRequired)
            {
                Action<string> d = new Action<string>(this.gsmModule_SerialPortIO);
                this.Invoke(d, new object[] { obj });
            }
            else
            {
                this.richTextBox_指令.Text += obj;
            }
        }

        private void button_关闭_Click(object sender, EventArgs e)
        {

            if (this.gsmModule.Close())
            {
                MessageBox.Show("关闭串口成功!");
                this.gsmModule = null;
            }
            else
                MessageBox.Show("关闭串口失败");

        }
        //我是中国人我是中国人我是中国人我是中国人我是中国人我是中国人我是中国人我是中国人我是中国人我是中国人我是中国人我是中国人我是中国人我是中国人

        private void button_发送_Click(object sender, EventArgs e)
        {
            try
            {
                bool result = false;
                if (this.checkBox_TEXT格式.Checked)
                    result = this.gsmModule.SendTEXT(this.textBox_手机号.Text, this.textBox_短信.Text,1000);
                else
                    result = this.gsmModule.SendPDU(this.textBox_手机号.Text, this.textBox_短信.Text,2000);

                if (result)
                    MessageBox.Show("发送成功");
                else
                    MessageBox.Show("发送失败");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void button_信号强度_Click(object sender, EventArgs e)
        {
            this.gsmModule.GetCSQ();
        }
    }
}
